package com.mccken.practice.top100.Top_41;

import com.mccken.practice.top100.TreeNode;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-04-02 13:31
 **/
public class SortedArrayToBST_108 {

	public TreeNode sortedArrayToBST(int[] nums) {

		return dfs(nums, 0, nums.length - 1);

	}

	private TreeNode dfs(int[] nums, int left, int right) {
		if (left > right) {
			return null;
		}

		// 以升序数组中间元素作为root
		int mid = left + (right - left) / 2;
		TreeNode root = new TreeNode(nums[mid]);
		// 递归构建root左右子树
		root.left = dfs(nums, left, mid - 1);
		root.right = dfs(nums, mid + 1, right);
		return root;

	}
}
